import { _decorator, Component, BaseNode, find, Prefab, instantiate, Label, NodeEventType, Animation, director, sys, Sprite, Color } from 'cc';
import Leves from '../../Data/Index/LevelS';
import { Data } from '../Types';
const { ccclass, property } = _decorator;

@ccclass('Index')
export class Index extends Component {
    static levels: boolean = false; //关卡显示
    static level: number = 1;   //关卡数
    static Xsize: number = 2;
    static Ysize: number = 2;
    Header: BaseNode;
    Main: BaseNode;
    @property(Prefab)
    item: Prefab;
    start() {
        let data: Data = JSON.parse(sys.localStorage.getItem('userData'));
        this.Header = find('Canvas/Herder');
        this.Main = find('Canvas/Main');
        let node = find('Canvas/Main/BG');
        //关卡大于2就显示继续
        console.log(data);
        if (data) {
            if (data.level >= 2) {
                this.Header.children[1].active = true;
            } else {
                this.Header.children[1].active = false;
            }
        } else {
            console.log('nosave');
        }
        //循环生成关卡按钮
        Leves.forEach((item) => {
            let children = instantiate(this.item);
            children.children[0].getComponent(Label).string = `第${item.name}关`;
            children.getComponent(Sprite).color = Color.GRAY;
            if (item.name <= data?.level) {
                children.on(NodeEventType.TOUCH_START, () => {
                    children.getComponent(Animation).play();
                    this.opMain(item.Xsize, item.Ysize, item.name);
                });
                children.getComponent(Sprite).color = Color.WHITE;
            } else if (item.name <= 1) {
                children.on(NodeEventType.TOUCH_START, () => {
                    children.getComponent(Animation).play();
                    this.opMain(item.Xsize, item.Ysize, item.name);
                });
                children.getComponent(Sprite).color = Color.WHITE;
            }
            node.addChild(children);
        });
    }

    update(deltaTime: number) {
        if (Index.levels) {
            this.Header.active = false;
            this.Main.active = true;
        } else {
            this.Header.active = true;
            this.Main.active = false;
        }
    }
    //打开Main场景
    opMain(Xsize: number = 2, Ysize: number = 2, level: number = 1) {
        Index.Xsize = Xsize;
        Index.Ysize = Ysize;
        Index.level = level;
        director.loadScene('Main');
    }
    //按钮监听
    click({ target }) {
        let node: BaseNode = target;
        switch (node.name) {
            case 'Continue': (() => {
                //继续关卡
                let data: Data = JSON.parse(sys.localStorage.getItem('userData'));
                Leves.forEach(item => {
                    if (item.name === data.level) {
                        this.opMain(item.Xsize, item.Ysize, item.name);
                    }
                })
            })(); break;
            case 'Start': this.opMain(2, 2, 1); break;
            case 'Level': Index.levels = true; break;
            case 'Break': Index.levels = false; break;
        }
    }
}


